<?php

require CAMINHO_SERVIDOR.'conexao.php';
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of timeDAO
 *
 * @author Fernando
 */
class timeDAO {

    private $con;

    function __construct() {
        self::setCon(conectar());
        mysql_query('SET CHARACTER SET utf8');
    }

    public function getCon() {
        return $this->con;
    }

    public function setCon($con) {
        $this->con = $con;
    }

    public function fecharConexao() {
        desconectar(self::getCon());
    }

    function executaQuery($query) {
        return mysql_query($query, self::getCon());
    }

    function executaQueryAll($query) {
        $posts = array();
        $result = mysql_query($query, self::getCon());

        if (mysql_num_rows($result)) {
            while ($post = mysql_fetch_assoc($result)) {
                $posts[] = $post;
            }
        }

        return $posts;
    }

    function executaQueryRow($query) {
        $result = mysql_query($query, self::getCon());
        return mysql_fetch_assoc($result);
    }

    function gravaTime($nome, $img) {
        $query = "INSERT INTO tb_time (nome, img) VALUES('" . $nome . "','" . $img . "')";
        $result = self::executaQuery($query);
        return $result;
    }

    function alteraTime($id, $nome, $img) {
        $query = "UPDATE tb_time SET nome='" . $nome . "', img='" . $img . "' WHERE id_time=" . $id;
        $result = self::executaQuery($query);
        return $result;
    }

    function listaAllTimes() {
        $query = "SELECT id_time,nome,img FROM tb_time";
        $result = self::executaQueryAll($query);
        return $result;
    }

    function getTimeById($id_time) {
        $query = "SELECT id_time,nome,img FROM tb_time WHERE id_time=" . $id_time;
        $result = self::executaQueryRow($query);
        return $result;
    }

    function getIdTimeByNome($nome) {
        $query = "SELECT COUNT(id_time) as total FROM tb_time WHERE nome='" . $nome . "'";
        $result = self::executaQueryRow($query);
        return $result['total'];
    }

    function buscaTimeByNome($nome) {
        $query = "SELECT * FROM tb_time WHERE nome='" . $nome . "'";
        $result = self::executaQueryAll($query);
        return $result;
    }
    function getTimeNome($nome) {
        $query = "SELECT * FROM tb_time WHERE nome='" . $nome . "'";
        $result = self::executaQueryRow($query);
        return $result;
    }

}
